#! usr/bin/python
import appuifw, e32, graphics, globalui, time

appuifw.app.title = u"Student Handler"   #creates application title

#set up text area for writing to
textArea = appuifw.Text()
textArea.font = 'normal'
textArea.color = 0x000000 # BLACK

appuifw.app.body = textArea   #makes the screen a text area, not a canvas
appuifw.app.screen = "normal"

dayList = list()     #this is the list of days
eventList = list()   #This list of events to display

#Define the exit function
def quit():
    app_lock.signal()
appuifw.app.exit_key_handler = quit

def handleRedraw(rect):
    c.blit(myimage)

def popDays():
   eventList.append({"subject": "cp3120", "type": "lecture", "from": "1300", "until": "1500", "location": "E1.103", "weekly": True})
   
   dayList.append({"day": "Monday", "event": list()})
   dayList.append({"day": "Tuesday", "event": list()})
   dayList.append({"day": "Wednesday", "event": list()})
   dayList.append({"day": "Thursday", "event": list()})
   dayList.append({"day": "Friday", "event": list()})
   dayList.append({"day": "Saturday", "event": list()})
   dayList.append({"day": "Sunday", "event": list()})

def addEvent():
   loopOp = True
   
   while loopOp == True:
      #userDay = appuifw.query(u"Day", "number") -1
      userDay = globalui.global_popup_menu([u"Monday",u"Tuesday",u"Wednesday",u"Thursday",u"Friday",u"Saturday",u"Sunday",],u"Day")
      #textArea.add(unicode(userDay))
      #textArea.add(unicode(dayList)) #debugging
      textArea.add(unicode(dayList[userDay]["day"] + " chosen"))
      textArea.add(u"\n")
      
      #the following will need error checking in the final thing for correct data input, but this is not the final thing
      eventSubject = appuifw.query(u"Event subject", "text")  #in GUI this will be a drop down box of available subjects
      #eventType = appuifw.query(u"Event type", "text")        #this will be selected as well in the GUI
      eventType = globalui.global_popup_menu([u"Lecture",u"Practical",u"Tutorial"],u"Type")  #User does not have to type it
      #if eventType = Lecture
      #  lecturer  = raw_input("Lecturer: ")
      eventFrom = appuifw.query(u"Start time: ", "time")
      eventUntil = appuifw.query(u"End time: ", "time")
      eventLocation = appuifw.query(u"Location: ", "text")
      eventWeekly = appuifw.query(u"Weekly: ", "query")

      eventEntry = {"subject": eventSubject, "type": eventType, "from": eventFrom, "until": eventUntil, "location": eventLocation, "weekly": eventWeekly}
      eventList.append(eventEntry)
      dayList[userDay]["event"].append(eventEntry)

      appuifw.note(u"Event added", "conf")
      loopOp = appuifw.query(u"Continue: ", "query")

def deleteEvent():
   targetDay = globalui.global_popup_menu([u"Monday",u"Tuesday",u"Wednesday",u"Thursday",u"Friday",u"Saturday",u"Sunday",],u"Day")
   targetEvent = dayList[targetDay]
#   for day in dayList:
#      if day["day"] == targetDay:
#         if len(day["event"]) == 0: #check if there is an event to delete
#                textArea.add(u"No event to delete")
#         else:
   textArea.add(u"Events:")
   textArea.add(u"\n")
   eventNo = 1
   for event in targetEvent["event"]:
      textArea.add(unicode(eventNo))
      textArea.add(u". ")
      textArea.add(unicode(event["subject"]))
      textArea.add(u", ")
      textArea.add(unicode(event["type"]))
      textArea.add(u", ")
      textArea.add(unicode(time.ctime(event["from"])))
      textArea.add(u"\n")
      eventNo += 1
               
   deleteOp = appuifw.query(u"Which do you want to delete: ", "number") - 1
   del (dayList[targetDay]["event"][deleteOp])
   textArea.add(u"\n")
   
def printDayList(daysOnly = True):
   for day in dayList:
      if daysOnly:
         textArea.add(u"Working day is: %s" % day["day"])
         textArea.add(u"\n")
      else:
         textArea.add(unicode(day))
         textArea.add(u"\n")

def printDaysEvents():
   workingDay = globalui.global_popup_menu([u"Monday",u"Tuesday",u"Wednesday",u"Thursday",u"Friday",u"Saturday",u"Sunday",],u"Day")
   workingEvent = dayList[workingDay]
   textArea.add(u"Data for working event: %s" % str(workingEvent["day"]))
   textArea.add(u"\n")
   try:
      for event in workingEvent["event"]:
         textArea.add(unicode(event["subject"]))
         textArea.add(u", ")
         textArea.add(unicode(event["type"]))
         textArea.add(u", ")
         textArea.add(unicode(time.ctime(event["from"])))
         textArea.add(u"\n")
   except KeyError, e:
      textArea.add(u"Selected day (%s) doesn't have any events. Code: %s" % (chosenDay["day"], e))
      textArea.add(u"\n")

popDays() #creates the database

appuifw.app.menu = [(u"Add Event", addEvent),(u"Delete Event", deleteEvent),(u"Print Event", printDaysEvents)] #adds left softkey options

app_lock = e32.Ao_lock()
app_lock.wait()
